package cn.dt70.service.impl;

import cn.dt70.dao.aqbProject.AqbProjectMapper;
import cn.dt70.pojo.AqbProject;
import cn.dt70.service.AqbProjectService;
import cn.dt70.utils.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AqbProjectServiceImpl implements AqbProjectService {

    private static final Logger LOGGER = Logger.getLogger(AqbProjectServiceImpl.class);

    @Resource
    private AqbProjectMapper aqbProjectMapper;

    @Override
    public boolean addProBase(AqbProject aqbProject) {
        boolean flag=false;

        try {
            int row = aqbProjectMapper.saveProBase(aqbProject);
            if (row==1){
                flag=true;
            }
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.addProBase -->"+e.getMessage());
        }

        return flag;
    }

    @Override
    public boolean modifyProject(AqbProject aqbProject) {
        boolean flag = false;

        try {
            int row=aqbProjectMapper.updateProject(aqbProject);
            if (row==1){
                flag=true;
            }
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.modifyProject -->"+e.getMessage());
        }

        return flag;
    }

    @Override
    public List<AqbProject> findReturnProjectList(Integer createrId, Integer pageIndex) {

        return findProjectList(createrId,null,3,4,pageIndex);
    }

    @Override
    public List<AqbProject> findAssessNeedProjectList(Integer pageIndex) {
        return findProjectList(null,null,0,null,pageIndex);
    }

    @Override
    public List<AqbProject> findAssessdProjectList(Integer pageIndex) {
        return findProjectList(null,null,1,null,pageIndex);
    }

    @Override
    public List<AqbProject> findProjectList(Integer createrId,Integer proType,Integer proStatus1,Integer proStatus2, Integer pageIndex) {
        List<AqbProject> aqbProjects = new ArrayList<>();
        Integer currentPageNo = 1;
        if (null!=pageIndex && !pageIndex.equals("")){
            currentPageNo=pageIndex;
        }
        PageSupport pageSupport = new PageSupport();

        try {
            Integer totalLogs = aqbProjectMapper.selectCountOfProject(createrId,proType,proStatus1,proStatus2);
            pageSupport.setTotalCount(totalLogs);
            pageSupport.setCurrentPageNo(currentPageNo);
            aqbProjects= aqbProjectMapper.selectAllProjects(createrId,proType,proStatus1,proStatus2,(pageSupport.getCurrentPageNo()-1)*pageSupport.getPageSize(),pageSupport.getPageSize());
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.findProjectList -->"+e.getMessage());
        }

        return aqbProjects;
    }

    @Override
    public AqbProject findOneProject(Integer proId,String proCode) {
        AqbProject aqbProject = new AqbProject();

        try {
            aqbProject=aqbProjectMapper.selectOneProject(proId,proCode);
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.findOneProject -->"+e.getMessage());
        }

        return aqbProject;
    }

    @Override
    public boolean modifyDetailIdOfProject(Integer detailId, Integer proId) {
        AqbProject tempProject = findOneProject(proId,null);
        tempProject.setProductId(detailId);

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyCurrentBids(Integer newBid, Integer proId) {
        AqbProject tempProject = findOneProject(proId,null);
        Integer oldCurrent = tempProject.getCurrentAmount();
        Integer insertCurrent = oldCurrent+newBid;
        tempProject.setCurrentAmount(insertCurrent);

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyAsseseDate(Date date, Integer proId) {
        AqbProject tempProject = findOneProject(proId,null);
        tempProject.setAssessDate(date);

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyFullDate(Date date, Integer proId) {
        AqbProject tempProject = findOneProject(proId,null);
        tempProject.setFullDate(date);

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyStatus(Integer status, Integer proId) {
        AqbProject tempProject = findOneProject(proId,null);
        tempProject.setProStatus(status);

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyPayMoneySum(BigDecimal newPayMoney, Integer proId) {
        AqbProject tempProject = findOneProject(proId,null);
        tempProject.setPayMoneySum(tempProject.getPayMoneySum().add(newPayMoney));

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyPayInterestSum(BigDecimal newPayInterest, Integer proId) {
        AqbProject tempProject = findOneProject(proId,null);
        tempProject.setPayInterestSum(tempProject.getPayInterestSum().add(newPayInterest));

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyPayableInterest(BigDecimal payableInterest, Integer proId) {
        AqbProject tempProject = findOneProject(proId,null);
        tempProject.setPayableInterest(payableInterest);

        return modifyProject(tempProject);
    }
}
